
Ext.define("Admin.ux.util.Util",{
    /**
     *  查找父节点数组
     * @param data
     * @param parentId
     * @param arr
     * @returns {*[]|*}
     */
     findParentId : function(data, parentId, parentKey = 'parentId', childKey = 'menuId') {
        let arr = []
        const item = data.find((item) => item[childKey] === parentId)
        if (item) {
            arr.push(item)
            if (item[parentKey]) {
                return this.findParentId(data, item[parentKey], arr, parentKey, childKey)
            }
        }
        return arr
    },
    /**
     * tree装换为list
     * @param tree
     * @returns {[]}
     */
    treeChangelist: function(tree,children = 'children') {
        const list = []
        const queue = [...tree]
        while(queue.length) {
            const node = queue.shift()
            const childrenNode = node[children]
            if(childrenNode) {
                queue.push(...childrenNode)
            }
            list.push(node)
        }
        return list
    },
    /**
     * list转换为Tree
     * @param list
     * @returns {*[]}
     */
    listChangeTree: function(list,key = 'id',parentId = 'parentId',children = 'children') {
        const [map, treeData] = [{}, []];

        for (let i = 0; i < list.length; i += 1) {
            map[list[i][key]] = i;
            list[i][children] = [];
        }

        for (let i = 0; i < list.length; i += 1) {
            const node = list[i];
            if (node[parentId] && list[map[node[parentId]]]) {
                list[map[node[parentId]]][children].push(node);
            } else {
                treeData.push(node);
            }
        }
        return treeData;
    }
});